package org.i_plus.model.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.i_plus.model.HobbyBean;
import org.i_plus.model.MemberBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HobbyDAOHibernate {
	private SessionFactory sessionFactory;
	public void setSessionFactory(SessionFactory sessionFactory)
	{
		this.sessionFactory=sessionFactory;
	}
	public List<HobbyBean> select(){
		Session ses= this.sessionFactory.getCurrentSession();
		Query query=ses.createQuery("from HobbyBean");
		List<HobbyBean> list =query.list();
		for(HobbyBean bean:list)
		{
			System.out.println(bean.getAccount());
			System.out.println(bean.isBall());
			System.out.println(bean.isRun());
		}
		return query.list();
	}
	public HobbyBean selectByAccount(String account)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		HobbyBean bean=(HobbyBean)ses.get(HobbyBean.class, account);
		System.out.print(bean.getAccount()+" ");
		System.out.println(bean.isBall());
		System.out.println(bean.isRun());
		return bean;
	}
	public void insert(HobbyBean bean)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		HobbyBean hb=new HobbyBean();
		hb.setAccount(bean.getAccount());
		hb.setBall(bean.isBall());
		hb.setRun(bean.isRun());
		hb.setReading(bean.isReading());
		hb.setSwim(bean.isSwim());
		hb.setMovie(bean.isMovie());
		hb.setSinging(bean.isSinging());
		hb.setBike(bean.isBike());
		hb.setTvgame(bean.isTvgame());
		ses.save(hb);
	}
	public void delete(String account)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		Query query=ses.createQuery("delete from HobbyBean where account=?");
		query.setParameter(0, account);
		query.executeUpdate();
	}
	public void update(HobbyBean bean)
	{
		Session ses=this.sessionFactory.getCurrentSession();
		System.out.println(bean.getAccount());
		HobbyBean hb=(HobbyBean)ses.get(HobbyBean.class, bean.getAccount());
		hb.setBall(bean.isBall());
		hb.setRun(bean.isRun());
		hb.setReading(bean.isReading());
		hb.setSwim(bean.isSwim());
		hb.setMovie(bean.isMovie());
		hb.setSinging(bean.isSinging());
		hb.setBike(bean.isBike());
		hb.setTvgame(bean.isTvgame());
		ses.save(hb);
	}
	public List<HobbyBean> findMany(String hql) {
		Session session=this.sessionFactory.getCurrentSession();
		Query query = session.createQuery(hql);   
        return query.list();
	}
	public static void main(String[] args) {
		//MemberDAOHibernate mh=new MemberDAOHibernate();
		
		
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		HobbyDAOHibernate hh = (HobbyDAOHibernate)context.getBean("HobbyDAO");
		hh.sessionFactory.getCurrentSession().beginTransaction();

//		HobbyBean bean = new HobbyBean();
//		bean.setAccount("a19897811");
//		bean.setBike(true);
//		bean.setMovie(true);
//		bean.setRun(true);
//		bean.setTvgame(true);
//		hh.insert(bean);
		
		HobbyBean bean = hh.selectByAccount("a19897811");
		System.out.println(bean.getAccount()+":"+bean.isBall()+":"+bean.isRun());

		hh.sessionFactory.getCurrentSession().getTransaction().commit();	
		}
}
